<?php 
defined('APP_PATH') or die ('Unauthorized access');

/**
 * 注册
 */
class RegController extends Controller
{
	public function __construct()
	{
		
	}
	
	public function index()
	{
		if($_SERVER['REQUEST_METHOD'] != "POST"){
			Error::message("请求非法！");
		}
	}
	
	public function reg()
	{
		$email = htmlspecialchars($this->request('email'));
		
		$password = $this->request('password');
		$rpassword = $this->request('rpassword');
		$vcode = $this->request('vcode');
		$nickname = htmlspecialchars(addslashes($this->request('nickname')));
		$sex = strtoupper($this->request('sex'));
		$sex = !in_array($sex,array("M","F")) ? "M" : $sex;
		if(empty($email))	exit('email_empty');
		if(empty($nickname)) exit('nickname_empty');
		if(!preg_match('/^[_.0-9a-z-]+@([0-9a-z][0-9a-z-]+.)+[a-z]{2,5}$/',$email))	exit('email_error');
		if(empty($password))	exit('password_empty');
		if($password != $rpassword)	exit("password_not_same");
		if($vcode != $_SESSION['code'])	exit("vcode_error");
		
		//验证用户是否已注册
		$user = Load::Model('user');
		if($user->checkUser($email))	exit("email_reged");
		
		$arr = array(
			"wb_email" => $email,
			"wb_password" => md5(md5($password)),
			"wb_nickname" => $nickname,
			"wb_sex" => $sex,
			"wb_regtime"=>time(),
			"wb_regip"=> $_SERVER['REMOTE_ADDR'],
			);
		$reg = $user->insert($arr);
		$login = $user->login($arr['wb_email'],$arr['wb_password']);
		echo $reg && $login ? 'success' : 'error';
		exit;
	}
	
	public function code()
	{
		$code = Load::Ext("vcode");
		header("Content-Type:image/png");
		$code->create(60,30);
	}
}